package com.micai.springboot.view;

import com.micai.springboot.base.bean.BeanUtilsBeanToMap;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/7/2 14:47
 */
public class PoiExcelExport<T> {

    // excle导出名称
    private String fileName;
    // excel 表头
    private String[] heads;
    // excel 列
    private String[] cols;
    // 设置数值型的列 从0开始计数
    private int[] numerics;
    //list集合
    private List<T> list;
    // 输出流
    private OutputStream out;

    // 每页显示数据条数，默认65535
    private static final Integer defaultCount = 1000;

    // 构造函数
    public PoiExcelExport(String fileName, String[] heads, String[] cols, List<T> list, OutputStream out) {
        this.fileName = fileName;
        this.heads = heads;
        this.cols = cols;
        this.list = list;
        this.out = out;
    }

    // 构造函数 带数字类型
    public PoiExcelExport(String fileName, String[] heads, String[] cols, List<T> list, int[] numerics, OutputStream out) {
        this.fileName = fileName;
        this.heads = heads;
        this.cols = cols;
        this.list = list;
        this.numerics = numerics;
        this.out = out;
    }

    public void exportExcel() {
        if (list != null && !list.isEmpty()) {
            // 创建一个excel对象
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();
            for (int i = 0; i <= (list.size() / defaultCount); i++) {
                // 工作表
                HSSFSheet hssfsheet = hssfworkbook.createSheet();
                // 工作表名称
                hssfworkbook.setSheetName(i, fileName.replace(".xls", "") + "(第" + (i + 1) + "页)");
                int beginRows = defaultCount * i;
                int endRows = (list.size() > defaultCount * (i + 1)) ? defaultCount * (i + 1) - 1 : list.size() - 1;
                HSSFRow hssfrowHead = hssfsheet.createRow(0);
                // 输出excel 表头
                if (heads != null && heads.length > 0) {
                    for (int h = 0; h < heads.length; h++) {
                        HSSFCell hssfcell = hssfrowHead.createCell(h, Cell.CELL_TYPE_STRING);
                        hssfcell.setCellValue(heads[h]);
                    }
                }
                // 要设置数值型 列表
                HSSFCellStyle cellstyle = hssfworkbook.createCellStyle();
                cellstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("##0"));
                // 是否是数值型
                boolean isnum = false;

                // 输出excel 数据
                for (int curRow = beginRows; curRow <= endRows; curRow++) {
                    // 获取数据
                    /*BeanToMap<T> btm = new BeanToMap<T>();*/
                    BeanUtilsBeanToMap btm = new BeanUtilsBeanToMap();
                    Map<String, Object> hm = (Map<String, Object>) btm.objectToMap(list.get(curRow));
                    // 创建excel行 表头1行 导致数据行数 延后一行
                    HSSFRow hssfrow = hssfsheet.createRow(curRow % defaultCount + 1);
                    // 读取数据值
                    for (int k = 0; k < cols.length; k++) {
                        HSSFCell hssfcell = hssfrow.createCell(k);
                        isnum = false;
                        for (int z = 0; z < numerics.length; z++) {
                            if (numerics[z] == k) {
                                isnum = true;
                                break;
                            }
                        }
                        if (isnum) {
                            if (hm.get(cols[k]) != null || !hm.get(cols[k]).equals("")) {
                                hssfcell.setCellStyle(cellstyle);
                                hssfcell.setCellValue(hm.get(cols[k]) == null ? "" : hm.get(cols[k]).toString().replace(",", ""));
                            }
                        } else {
                            hssfcell.setCellValue(hm.get(cols[k]) == null ? "" : hm.get(cols[k]).toString());
                        }
                    }
                }

            }

            // excel生成完毕，写到输出流
            try {
                hssfworkbook.write(out);
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
